package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.BuyerTicketBO;
import cn.icanci.ladybird.pojo.bo.BuyerTicketFromBuyPageBO;
import cn.icanci.ladybird.pojo.dto.UserBuyerTicketDTO;
import cn.icanci.ladybird.service.user.UserBuyerTicketService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author icanci
 * @author: zxp
 * @date: Created in 2020/10/3 0003 下午 14:48
 * @classAction: 常用购票人数据展示
 */
@SuppressWarnings("all")
@Slf4j
@Controller
@RequestMapping("/api/v1/pri/user")
public class UserBuyerTicketController {

    @Autowired
    private UserBuyerTicketService userBuyerTicketService;
    @Autowired
    private UserBuyerTicketController userBuyerTicketController;
    @Autowired
    private UserOrderController userOrderController;

    @ResponseBody
    @RequestMapping("/buyerticket")
    public JsonData getUserIndex(Long userInfoId) {
        List<UserBuyerTicketDTO> userBuyerTicketDTO = userBuyerTicketService.queryUserBuyerTicketDTOByUserInfoId(userInfoId);
        return JsonData.builderSuccess(userBuyerTicketDTO);
    }


    /**
     * 跳转到 ticket-list 票列表
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/buyerticket-list")
    public String userTicketList(Model model, HttpServletRequest request, Integer pageNum, Integer pageSize) {
        // 页面参数，然后在Service进行分页
        Long userId = Long.valueOf(request.getSession().getAttribute("user_id").toString());
        PageInfo<UserBuyerTicketDTO> userBuyerTicketDTOPageInfo = userBuyerTicketService.queryAllByNotObject(userId, pageNum, pageSize);
        JsonData jsonData = JsonData.builderSuccess(userBuyerTicketDTOPageInfo);
        model.addAttribute("jsonData", jsonData);
        return "user/people-center";
    }

    @Deprecated
    @RequestMapping("/json/buyerticket")
    public String getUserIndexJson(Model model, Long userInfoId) {
        // 查询列表
        List<UserBuyerTicketDTO> userBuyerTicketDTO = userBuyerTicketService.queryUserBuyerTicketDTOByUserInfoId(userInfoId);
        ArrayList<Object> list = new ArrayList<>();
        list.add(userBuyerTicketDTO);
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/people-center";
    }

    /**
     * 用户在订单页面
     *
     * @param model         视图对象
     * @param buyerTicketBO 传递参数对象
     * @return 返回视图
     */
    @PostMapping("/buyerticket-add")
    public String userAddInsert(Model model, HttpServletRequest request, BuyerTicketBO buyerTicketBO) {
        log.info("UserBuyerTicketController.userAddInsert - " + buyerTicketBO);
        userBuyerTicketService.insertById(buyerTicketBO);
        return userTicketList(model, request, null, null);
    }

    /**
     * 用户在buy页面添加常用购票人
     *
     * @param model                    视图对象
     * @param request                  HttpServletRequest 对象
     * @param buyerTicketFromBuyPageBO BO 对象
     * @return 返回到订单页面
     */
    @PostMapping("/buyerticket-add-buy")
    public String userAddBuyerTicket(Model model, HttpServletRequest request, BuyerTicketFromBuyPageBO buyerTicketFromBuyPageBO) {
        BuyerTicketBO buyerTicketBO = new BuyerTicketBO();
        log.info("UserBuyerTicketController.userAddBuyerTicket - " + buyerTicketFromBuyPageBO);
        buyerTicketBO.setBuyerTicketUsername(buyerTicketFromBuyPageBO.getBuyerTicketUsername());
        buyerTicketBO.setBuyerTicketDocumentType(buyerTicketFromBuyPageBO.getBuyerTicketDocumentType());
        buyerTicketBO.setBuyerTicketIdCardNumber(buyerTicketFromBuyPageBO.getBuyerTicketIdCardNumber());
        buyerTicketBO.setUserInfoId(buyerTicketFromBuyPageBO.getUserInfoId());
        log.info("UserBuyerTicketController.userAddBuyerTicket - " + buyerTicketBO);
        userBuyerTicketService.insertById(buyerTicketBO);
        return userOrderController.userToBuyPage(model, request, buyerTicketFromBuyPageBO.getOrderId());
    }
}
